****************************************
*Table XXX: Robots and college enrollment by race and gender*
****************************************

*Main outcomes from Census
use "$final_data_outcomes/czone_Students.dta", clear
merge 1:1 czone year using "$final_data_outcomes/IPEDS_EnrollmentCZ.dta", assert(1 3) nogenerate
merge 1:1 czone year using "$final_data_outcomes/czone_StudentsDemographics.dta", assert(1 3) nogenerate

*Automation
merge 1:1 czone year using "$final_data_automation/czones_ExposureAutomation.dta", assert(1 3) nogenerate

*Imports
merge 1:1 czone year using "$final_data_imports/czone_ExposureChina", assert(1 3) nogenerate

*Covariates
merge m:1 czone using "$final_data_covariates/czone1990_Covariates.dta", assert(3) nogenerate
merge m:1 czone using "$final_data_covariates/czone1970_Covariates.dta", assert(3) nogenerate
merge m:1 czone using "$final_data_covariates/IPEDS_institutionControlsCZ.dta", keep(1 3) nogenerate

xtset czone year

*Keep periods of interest
keep if year<=2000

foreach var in public communityCollege uniTop20 uniTop30Dummy uniTop30 largest150 forProfit nonProfit numberOfUni private {
replace `var' = 0 if mi(`var')
}

egen medianAnyAid = median(anyAid), by(statefip)
	replace anyAid = medianAnyAid if mi(anyAid)
	
gen initialShare_m =  sc_m_1990
	replace initialShare_m = sc_m_2000 if year == 2000

gen initialShare_f =  sc_f_1990
	replace initialShare_f = sc_f_2000 if year == 2000

gen initialShare_w =  sc_w_1990
	replace initialShare_w = sc_w_2000 if year == 2000

gen initialShare_nw =  sc_nw_1990
	replace initialShare_nw = sc_nw_2000 if year == 2000

* summary 
su sc_f_1990 sc_f_2000 initialShare_f [w=ipums_pop_1990]
su sc_m_1990 sc_m_2000 initialShare_m [w=ipums_pop_1990]

*Create confidence intervals

gen coef =.
gen se =.
gen ci_u =.
gen ci_d =.


*Regression coefficients
local i = 1
foreach type in m f w nw {
	
eststo: ivreghdfe d_sc_`type' (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_`type'_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

replace coef = _b[expof_us_adj] in `i'
replace se = _se[expof_us_adj] in `i'
replace ci_u = coef + 1.96*se in `i'
replace ci_d = coef - 1.96*se in `i'

local i = `i'+1

}

*keep if _n<=6
keep if _n<=4
keep coef* se* ci*


gen position=0 if _n==1
replace position=2  if _n==2
replace position=5  if _n==3
replace position=7  if _n==4

*Figure: robots and schooling by demographics
twoway  (bar coef position, barwidth(1.2) fcolor(navy) msymbol(diamond) msize(small)) ///
		(scatter coef position, color(black) msymbol(diamond) msize(small)) ///
		(rcap ci_u ci_d position, lcolor(black) lwidth(vthin)) ///
		, ylabel(0(.2)1., angle(horiz) labsize(medium) grid gmin gmax format(%5.2f)) yline(0, lcolor(black) lwidth(vthin) lpattern(shortdash)) xtitle("")  ///
		legend(off) ysize(2.5) xsize(4) ytitle("US robot exposure" "point estimate", size(medium)) ///
		xscale(noline) xscale(alt) xlabel(-1 " " 0 `" "Men" " " "[1]" "' 2 `" "Women" " " "[2]" "' 5 `" "Whites" " " "[3]" "' 7 `" "Non-whites" " " "[4]" "'  8 " ", labsize(medium) noticks) ///
		text(1.35 1 "Gender", place(c) size(medium) color(black)) ///
		text(1.35 6 "Race and ethnicity", place(c) size(medium) color(black)) ///
		text(1.32 1 "_______________________", place(c) size(medium) color(black)) ///
		text(1.32 6 "_______________________", place(c) size(medium) color(black)) ///
		text(1.18 0 "__________", place(c) size(medium) color(black)) ///
		text(1.18 2 "__________", place(c) size(medium) color(black)) ///
		text(1.18 5 "__________", place(c) size(medium) color(black)) ///
		text(1.18 7 "__________", place(c) size(medium) color(black)) ///
		graphregion(color(white) fcolor(white) margin(r+20 t+10))
		graph export "$figures_main/Figure 2b.png", replace
		



/*
twoway  (bar coef position, barwidth(1.2) fcolor(navy) msymbol(diamond) msize(small)) ///
		(scatter coef position, color(black) msymbol(diamond) msize(small)) ///
		(rcap ci_u ci_d position, lcolor(black) lwidth(vthin)) ///
		, ylabel(-.2(.2)1.2, angle(horiz) labsize(medium) grid gmin gmax format(%5.2f)) yline(0, lcolor(black) lwidth(vthin) lpattern(shortdash)) xtitle("")  ///
		legend(off) ysize(2.5) ytitle("Weighted robot exposure" "point estimate", size(medium)) ///
		xscale(noline) xscale(alt) xlabel(-1 " " 0 `" "Men" " " "[1]" "' 2 `" "Women" " " "[2]" "' 5 `" "Whites" " " "[3]" "' 7 `" "Non-whites" " " "[4]" "' 10 `" "Natives" " " "[5]" "' 12 `" "Immigrants" " " "[6]" "'  13 " ", labsize(medium) noticks) ///
		text(1.88 1 "Gender", place(c) size(medium) color(black)) ///
		text(1.88 6 "Race and ethnicity", place(c) size(medium) color(black)) ///
		text(1.88 11 "Birthplace", place(c) size(medium) color(black)) ///
		text(1.8 1 "_______________________", place(c) size(medium) color(black)) ///
		text(1.8 6 "_______________________", place(c) size(medium) color(black)) ///
		text(1.8 11 "_______________________", place(c) size(medium) color(black)) ///
		text(1.63 0 "__________", place(c) size(medium) color(black)) ///
		text(1.63 2 "__________", place(c) size(medium) color(black)) ///
		text(1.63 5 "__________", place(c) size(medium) color(black)) ///
		text(1.63 7 "__________", place(c) size(medium) color(black)) ///
		text(1.63 10 "__________", place(c) size(medium) color(black)) ///
		text(1.63 12 "__________", place(c) size(medium) color(black)) ///
		graphregion(color(white) fcolor(white) margin(r+20 t+10))
		graph export "${figures_output}/robots_school_demographics.png", replace
		

		
		
		
		
